package com.itts.middle.modules.project.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itts.common.entity.Project;
import com.itts.middle.modules.project.mapper.ProjectMapper;
import com.itts.middle.modules.project.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Autowired
    ProjectMapper projectMapper;

    /**
     * 基于注解sentinel 限流控制
     * @return
     */
    @SentinelResource(value = "selectList",
            blockHandler = "handleFlowQpsException",//流量控制
            fallback = "queryOrderInfo2Fallback"    //fobolake
    )
    @Override
    public List<Project> selectList() {

        ProjectService projectService = new ProjectServiceImpl();

        List<Project> list = projectMapper.pagelist(100021);
        list.forEach(e->{

        });

        List<Project> pagelist = projectMapper.pagelist(100021);
        List<Project> projects = ProjectServiceImpl.this.sort(pagelist,"id");
        /*try {
            //反射
            Class<ProjectServiceImpl> aClass = ProjectServiceImpl.class;
            projects = aClass.newInstance().sort(pagelist);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        return projects;
    }


    /**
     * 实现限流的异常处理
     * 默认情况下 , sentinel 对控制资源的限流处理是直接抛出异常, 这样对用户交流不友好, 需要处理异常信息
     *  处理不及时处理的业务, 可以写入数据库, 日志  后续操作读取补齐
     * @return
     */
    static String handleFlowQpsException(BlockException e) {
        e.toString();
        return "handleFlowQpsException for queryOrderInfo2: ";
    }
    static String queryOrderInfo2Fallback(BlockException e) {
        e.toString();
        return "fallback queryOrderInfo2: ";
    }
}
